package com.example.oracle.dao;

import com.example.oracle.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

import static org.apache.ibatis.type.JdbcType.TIMESTAMP;

@Mapper
public interface UserDao {


    @InsertProvider(type = UserSqlProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    //@Options注解 在插入记录时，一般是定义主键自增(auto_increment)，但是在某些情况下，我们插入一条记录后，还想得到这条记录的自增主键ID，useGeneratedKeys=true就是定义数据库返回主键ID的，
    int insert(User user);

    @Delete("DELETE FROM users WHERE id =#{id}")
    int delete(Long id);


    @UpdateProvider(type = UserSqlProvider.class, method = "update")
    int update(User user);


    @Select("SELECT * FROM users WHERE id = #{id}")
    // 返回 Map 结果集
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "name", column = "name"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "role", column = "role"),
            @Result(property = "createTime", column = "create_time", jdbcType = TIMESTAMP),
            @Result(property = "createIp", column = "create_ip"),
            @Result(property = "lastTime", column = "last_time"),
            @Result(property = "lastIp", column = "last_ip"),
            @Result(property = "errorTimes", column = "error_times"),
            @Result(property = "status", column = "status"),
            @Result(property = "sprotsmanId", column = "sprotsman_id"),
            @Result(property = "locationId", column = "location_id"),
            @Result(property = "organizationId", column = "organization_id"),
    })
    User findById(Long id);


    @SelectProvider(type = UserSqlProvider.class, method = "getList")
    // 返回 Map 结果集
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "name", column = "name"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "role", column = "role"),
            @Result(property = "createTime", column = "create_time", jdbcType = TIMESTAMP),
            @Result(property = "createIp", column = "create_ip"),
            @Result(property = "lastTime", column = "last_time"),
            @Result(property = "lastIp", column = "last_ip"),
            @Result(property = "errorTimes", column = "error_times"),
            @Result(property = "status", column = "status"),
            @Result(property = "sprotsmanId", column = "sprotsman_id"),
            @Result(property = "locationId", column = "location_id"),
            @Result(property = "organizationId", column = "organization_id"),
    })
    List<User> getList(Map<String, Object> params);

}